 	
 $(document).ready(function() {
 	        //		权限判断
		if($.cookie('ID') == null){
			window.location.href="http://www.duoweish.com/crm/login.html";
		}
//		2，3号权限
		if($.cookie('Right') == 1){
			window.location.href="http://www.duoweish.com/crm/index.html";
		}
		$('.dw_name').text($.cookie('Name'));
		$('.dw_part').text($.cookie('Department'));
            $(".select2").select2();
            $('#datepicker').datetimepicker({
                format:"YYYY-MM-DD",
                locale:moment.locale('zh-cn')
            });
            //初始化
            $('#user_select').html('');
             $.ajax({
	       	type:"get",
	       	url:"http://www.duoweish.com/apis/SalesInfo/",
	       	async:true,
	       	success:function(data){
	       		for (var i =0;i<data[1].length;i++) {
	       			var Option = `<option value="${data[1][i]}">${data[1][i]}</option>`;
	       			$('#Project').append(Option);
	       		}
	       		$('#user_select').html('');
	       		for (var i=0;i<data[0].length;i++) {
	       			var Option = `<option>${data[0][i]}</option>`;
	       			$('#user_select').append(Option);
	       		}
	       		var val = $('#Project').select2('val')
	       		 $.getJSON("http://www.duoweish.com:9300/DwProjectAjax/",{'name':val},function(ret) {
                    $('#Product').empty();
                    if(ret.length>0&&ret!='success'){
                    	for(var i = 0;i < ret.length;i++){
                        $('#Product').append("<option value=\'"+ret[i]+"\'>" + ret[i] + "</option>");
                    }
                    }
                    
                });
	       	}
	       });
            $('#Project').on("select2:select",function(){
                $.getJSON("http://www.duoweish.com:9300/DwProjectAjax/",{'name':$(this).val()},function(ret) {
                    $('#Product').empty();
                    for(var i = 0;i < ret.length;i++){
                        $('#Product').append("<option value=\'"+ret[i]+"\'>" + ret[i] + "</option>");
                    }
                });
            });
            $('#Product').change(function(){
                var name = "";
                $('#Product option:selected').each(function(){
                    name += $(this).text()+",";
                });
                $.getJSON("http://www.duoweish.com:9300/DwProductAjax/",{'name':name},function(ret) {
                    $('#table_body').empty();
                    for(var i = 0;i < ret.length;i++){
                        var tr = "<tr>\n" +
                            "<td style=\"line-height:35px;\">"+ret[i][0]+"</td>\n" +
                            "<td style=\"line-height:35px;\">"+ret[i][1]+"</td>\n" +
                            "<td style=\"line-height:35px;\">"+ret[i][2]+"</td>\n" +
                            "<td style=\"line-height:35px;\">"+ret[i][3]+"</td>\n" +
                            "<td style=\"line-height:35px;\">"+ret[i][4]+"</td>\n" +
                            "<td style=\"line-height:35px;\">"+ret[i][5]+"</td>\n" +
                            "<td><div class=\"input-group\">\n" +
                            "<div class=\"input-group-addon\">\n" +
                            "<i class=\"ion ion-bag\"></i>\n" +
                            "</div>\n" +
                            "<input type=\"text\" class=\"form-control\">\n" +
                            "</div>\n" +
                            "</td>\n" +
                            "<td><div class=\"input-group\">\n" +
                            "<div class=\"input-group-addon\">\n" +
                            "<i class=\"fa fa-rmb\"></i>\n" +
                            "</div>\n" +
                            "<input type=\"text\" class=\"form-control\">\n" +
                            "</div>\n" +
                            "</td>\n" +
                            "</tr>";
                        $('#table_body').append(tr);
                    }
                });
            })
           
            $(".creat_submit").click(function(){
            		  	var proData=[];
            		  	var all_inp = $('#table_body').find("input");
            		  	var submit_off = true;
            		  	for (var i=0;i<all_inp.length;i++) {
            	            	if(all_inp.eq(i).val()==""){
            					   submit_off=false;
            				    }
            	            }
            		  		if(submit_off){
										var User_sel = $("#user_select").select2("data")[0].text;
										var Item_sel =$("#Project").select2("data")[0].text;
										for (var i = 0;i<$("#table_body").find("tr").length;i++) {
											for (var j = 0;j<8;j++) {
												if(j==0){
												  proData.push( $("#table_body").find("tr").eq(i).find("td").eq(j).text());
											    proData.push('_');
											   }
												if(j==6||j==7){
											   	proData.push( $("#table_body").find("tr").eq(i).find("td").eq(j).find("input").val());
											   	proData.push('_');
											   
											   }
											}
											if(i!=$(".pro_data").find("tr").length-1){
											    proData.push('*@*');
												
											}
											
										}
										
										
										if (proData=='') {
						  					layer.msg('请填写完整的信息'); 				
															
										}else{
											$.ajax({
											type:"get",
											url:"http://www.duoweish.com/apis/SalesSubmit/",
											data:{
												UserId:$.cookie('ID'),
												Cstm:User_sel ,
												Project:Item_sel ,
												Product:proData.join(''),
												Time:$('#datepicker').val(),
												Kind:'2'
											},
											success:function(){
						  							
												layer.msg('提交成功',{time:1000},function(){location.reload()})
											
											}
										});
								}
							}else{
								layer.msg('信息填写不完整')
							}
            	
            })
         });